const svc = require('../util/service')
const logger = require('log4js').getLogger()
const config = require('../util/config')

module.exports = (req, res) => {
    svc.handlePost(req, res, (reqObj, resolve) => {
        const mongoMng = require('../mng/mongoMng')
        if (!mongoMng.db) {
            logger.error('[' + __filename + ':' + __line + '] 未连接mongo')
            resolve({
                result: -1,
                msg: '系统错误'
            })
            return
        }
        var promises = [new Promise(resolve => {
            mongoMng.db.collection('bill').aggregate([{
                $match: {
                    customer_phone: req.session.customer.phone
                }
            }, {
                $group: {
                    _id: null,
                    returnMoney: {
                        $sum: '$returnMoney'
                    }
                }
            }]).toArray((err, result) => {
                if (err) {
                    logger.error('[' + __filename + ':' + __line + ']', err)
                    resolve(0)
                    return
                }
                if (result.length == 0) {
                    resolve(0)
                    return
                }
                resolve(result[0].returnMoney)
            })
        })]
        if (req.session.customer.isPromoter) {
            promises.push(new Promise(resolve => {
                mongoMng.db.collection('user').find({
                    promoter: req.session.customer.phone
                }).toArray((err, result) => {
                    if (err) {
                        logger.error('[' + __filename + ':' + __line + ']', err)
                        resolve(0)
                        return
                    }
                    var customer_phones = []
                    result.forEach(item => {
                        customer_phones.push(item.phone)
                    })
                    mongoMng.db.collection('bill').find({
                        customer_phone: {
                            $in: customer_phones
                        },
                        returnTime: {
                            $ne: null
                        }
                    }).toArray((err, result) => {
                        if (err) {
                            logger.error('[' + __filename + ':' + __line + ']', err)
                            resolve(0)
                            return
                        }
                        let returnPromoterMoney = 0
                        let willReturnPromoterMoney = 0
                        result.forEach(item => {
                            if (item.returnPromoterMoney)
                                returnPromoterMoney += item.returnPromoterMoney
                            else
                                willReturnPromoterMoney += item.returnMoney / config.returnRate * config.returnPromoterRate
                        })
                        resolve({
                            returnPromoterMoney: Math.round(returnPromoterMoney * 100) / 100,
                            willReturnPromoterMoney: Math.round(willReturnPromoterMoney * 100) / 100
                        })
                    })
                })
            }))
        }
        Promise.all(promises).then(result => {
            var obj = {
                result: 1,
                returnMoney: result[0]
            }
            if (result.length > 1)
            {
                obj.returnPromoterMoney = result[1].returnPromoterMoney
                obj.willReturnPromoterMoney = result[1].willReturnPromoterMoney
            }
            resolve(obj)
        })
    })
}
